﻿using System;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Controls;
using RapidRepository.Cache;
using RapidRepository.TestMobileApplication.Entities;
using RapidRepository.TestMobileApplication.Repositories;
using RapidRepository.TestMobileApplication.View;

namespace RapidRepository.TestMobileApplication
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
                       
             EntityCache.EagerLoad<Customer>();
           // EntityCache.NoCache<Customer>();
            RapidRepository<Customer>.AddView<CustomerSearchView>(x => 
                new CustomerSearchView { FirstName = x.FirstName, LastName = x.LastName, PostCode = x.Address.PostCode })
                .AddFilter(x =>x.FirstName.Contains("a"));
        }

        private void AddCustomer(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddCustomer.xaml", UriKind.Relative));
        }

        private void CustomerSearch(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/CustomerSearch.xaml", UriKind.Relative));
        }

        #region Load Test Data

        private void LoadTestData(object sender, System.Windows.RoutedEventArgs e)
        {
            LoadRandomCustomers();
        }

        private void LoadRandomCustomers()
        {
            CustomerRepository rep = new CustomerRepository();

            for (int i = 0; i < 100; i++)
            {
                rep.Add(CreateCustomer());
            }
        }

        private static Customer CreateCustomer()
        {
            Random random = new Random();

            var customer = new Customer()
            {
                FirstName = GetFirstName(random.Next(0, firstNames.Length - 1)),
                LastName = GetLastName(random.Next(0, lastNames.Length - 1)),
                Id = Guid.NewGuid(),
                Address = new Address { PostCode = GetPostCode(random.Next(0, postCodes.Length - 1)), HouseNumber = random.Next() },
                ContactNumbers = new System.Collections.Generic.List<ContactNumber>()
            };
            customer.ContactNumbers.Add(new ContactNumber() { Number = random.Next().ToString(), ContactNumberType = ContactNumberType.Landline });
            customer.ContactNumbers.Add(new ContactNumber() { Number = random.Next().ToString(), ContactNumberType = ContactNumberType.Mobile });
            return customer;
        }

        #region Random Test Data

        private static string GetFirstName(int index)
        {
            return firstNames[index];
        }

        private static string GetLastName(int index)
        {
            return lastNames[index];
        }

        private static string GetPostCode(int index)
        {
            return postCodes[index];
        }

        static string[] firstNames = new string[] 
        { 
            "Andrew", 
            "Bill", 
            "Christopher", 
            "David", 
            "Edward", 
            "Frank", 
            "Gilly", 
            "Harold", 
            "Ian",
            "John",
            "Kevin",
            "Lee",
            "Michael",
            "Neil",
            "Owen",
            "Peter",
            "Quentin",
            "Robin",
            "Sean",
            "Thomas",
            "Una",
            "Victoria",
            "Wayne",
            "Xavier",
            "Yachin",
            "Zane"
        };
        static string[] lastNames = new string[] 
        { 
            "Atkins", 
            "Bennet", 
            "Cowley", 
            "Davidson", 
            "Ealing", 
            "Fox", 
            "Green", 
            "Harrison", 
            "Ince",
            "Jonas",
            "King",
            "Lane",
            "McDougal",
            "Noble",
            "O'Neil",
            "Peterson",
            "Riven",
            "Shilton",
            "Thornton",
            "Valance",
            "Winters"
        };

        static string[] postCodes = new string[]
        {
            "SW1 1AA",
            "RH55 2EE",
            "TF22 123",
            "YT5 3FD",
            "R1 4DE",
            "5TY PDD",
            "QZ1 3XD",
            "5T6 HGF"
        };

        #endregion 

        #endregion
    }
}